package studio7i.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.Date;
import studio7i.excepcion.DAOExcepcion;
import studio7i.modelo.Reserva;
import studio7i.negocio.ReservaNegocio;


/**
 * Servlet implementation class ReservaListado
 */
@WebServlet("/ReservaListado")
public class ReservaListado extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReservaListado() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		ReservaNegocio negocio = new ReservaNegocio();		
		try {
			String fechaInicio = request.getParameter("fechaini");
			String fechaFin = request.getParameter("fechafin");
			Integer idSala=Integer.parseInt(request.getParameter("sala"));

			java.sql.Date sqlFechaInicio = java.sql.Date.valueOf(fechaInicio);
			java.sql.Date sqlFechaFin = java.sql.Date.valueOf(fechaFin);
		
			Collection<Reserva> reservas = negocio.consultarReserva(sqlFechaInicio, sqlFechaFin, idSala);

			// Guardar en el ambiente de request
			// request.setAttribute("reservas", reservas);
			HttpSession session = request.getSession();
			session.setAttribute("reservas", reservas);
			RequestDispatcher rd = request.getRequestDispatcher("buscarReserva.jsp");
			rd.forward(request, response);

		} catch (DAOExcepcion e) {
			System.out.println(e.getMessage());
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}

	}

}
